//
//  BWH_TextView.m
//  bwh_textView
//
//  Created by 李小龙 on 2016/11/18.
//  Copyright © 2016年 bianwenhui. All rights reserved.
//

#import "BWHTextView.h"

@implementation BWHTextView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.font = [UIFont systemFontOfSize:16.f];
        self.textColor = [UIColor blackColor];
        self.layer.borderColor = [UIColor colorWithWhite:0.6 alpha:1.0].CGColor;
        self.layer.cornerRadius = 5.0f;
        self.layer.borderWidth = 0.65f;
        self.contentMode = UIViewContentModeRedraw;
        self.dataDetectorTypes = UIDataDetectorTypeNone;
        self.returnKeyType = UIReturnKeySend;
        self.enablesReturnKeyAutomatically = YES;
        
        [self setupPlaceHolder];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupPlaceHolder];
}

- (void)setupPlaceHolder
{
    _placeHolderText = nil;
    _placeHolderTextColor = [UIColor lightGrayColor];
    
    [self _addTextViewNotificationObservers];
}

- (void)dealloc
{
    [self _removeTextViewNotificationObservers];
}


#pragma mark -- Setters
- (void)setPlaceHolderText:(NSString *)placeHolderText
{
    _placeHolderText = [placeHolderText copy];
    [self setNeedsDisplay];
}

- (void)setPlaceHolderTextColor:(UIColor *)placeHolderTextColor
{
    _placeHolderTextColor = placeHolderTextColor;
    [self setNeedsDisplay];
}

-(void)setPlaceHolderTextFont:(UIFont *)placeHolderTextFont
{
    _placeHolderTextFont = placeHolderTextFont;
    [self setNeedsDisplay];
}


#pragma mark -- Drawing
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    if ([self.text length] == 0 && self.placeHolderText) {
        [self.placeHolderTextColor set];
        [self.placeHolderText drawInRect:CGRectInset(rect, 7.0f, 7.5f) withAttributes:[self _placeholderTextAttributes]];
    }
}

- (NSDictionary *)_placeholderTextAttributes
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    paragraphStyle.alignment = self.textAlignment;
    
    return @{ NSFontAttributeName : self.font,
              NSForegroundColorAttributeName : self.placeHolderTextColor,
              NSParagraphStyleAttributeName : paragraphStyle };
}


- (void)_addTextViewNotificationObservers
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_didReceiveTextViewNotification:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_didReceiveTextViewNotification:)
                                                 name:UITextViewTextDidBeginEditingNotification
                                               object:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_didReceiveTextViewNotification:)
                                                 name:UITextViewTextDidEndEditingNotification
                                               object:self];
}

- (void)_didReceiveTextViewNotification:(NSNotification *)notification
{
    [self setNeedsDisplay];
}

- (void)_removeTextViewNotificationObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidChangeNotification
                                                  object:self];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidBeginEditingNotification
                                                  object:self];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UITextViewTextDidEndEditingNotification
                                                  object:self];
    
}




/**
 计算行数，带换行符的不太准确
 */
- (NSUInteger)numberOfLinesOfText{
    return [BWHTextView numberOfLinesForMessage:self.text];
}

+ (NSUInteger)numberOfLinesForMessage:(NSString *)text{
    return (text.length / [BWHTextView maxCharactersPerLine]) + 1;
}

+ (NSUInteger)maxCharactersPerLine{
    return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)? 33:109;
}








@end
